package com.ladlee.parking.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ladlee.parking.entity.SysParking;
import com.ladlee.parking.entity.dto.ParkingDto;
import com.ladlee.parking.entity.vo.ParkingVo;
import com.ladlee.parking.service.SysParkingService;
import com.ladlee.parking.util.R;
import com.ladlee.parking.validate.group.Update;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@RestController
@RequestMapping(value = "/parking")
public class ParkingController {

    @Autowired
    SysParkingService sysParkingService;

    @GetMapping(value = "/index")
    public ModelAndView index() {
        return new ModelAndView("parking/parkingList");
    }

    @PostMapping(value = "/list")
    public R list(@RequestBody ParkingDto parking) {
        Page<ParkingVo> parkingPage = sysParkingService.parkingPage(parking.getKey(), parking.getPage(), parking.getLimit());
        return R.ok(parkingPage);
    }

    @GetMapping(value = "/updatePage/{parkingId}")
    public ModelAndView updatePage(@PathVariable(value = "parkingId") Integer parkingId) {
        ModelAndView modelAndView = new ModelAndView("parking/updateParking");
        SysParking sysParking = sysParkingService.getById(parkingId);
        modelAndView.addObject("info", sysParking);
        return modelAndView;
    }

    @PostMapping(value = "/update")
    public R update(@RequestBody @Validated(Update.class) ParkingVo parking) {
        SysParking sysParking = new SysParking();
        BeanUtil.copyProperties(parking, sysParking);
        return R.ok(sysParkingService.updateById(sysParking));
    }

    @GetMapping(value = "/delete/{parkingId}")
    public R delete(@PathVariable Integer parkingId) {
        return R.ok(sysParkingService.removeById(parkingId));
    }
}
